#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>

void matrix_init(MATRIX* this, int w, int h){
  int i,j;
  this->data = malloc(sizeof(double)*w);
  for(i=0;i<w;i++){
    this->data[i] = malloc(sizeof(double)*h);
  }
  this->w = w;
  this->h = h;

  //initialize to zeros;
  for(i=0;i<this->h;i++){
   for(j=0;j<this->w;j++){
     this->data[j][i] = 0;
   }
  }
}

void matrix_mult(MATRIX* l, MATRIX* r, MATRIX* d){
  int i,j,x;
  for(i=0; i < l->h; i++){
    for(j=0;j < r->w; j++){
      d->data[j][i] = 0;
        for(x=0;x<l->w;x++){
          d->data[j][i] += l->data[x][i] * r->data[j][x];
        }
    }
  }
}

void matrix_load_identity(MATRIX* this){
  int i,j;
  for(i=0;i<this->h;i++){
    for(j=0;j<this->w;j++){
      if(i == j){
        this->data[j][i] = 1;
      } else {
        this->data[j][i] = 0;
      }
    }
  }
}

void matrix_print(MATRIX* this){
  int i,j;
  for(i=0;i<this->h;i++){
    for(j=0;j<this->w;j++){
      printf("[%.3f]",this->data[j][i]);
    }
    printf("\n");
  }
}
void matrix_copy(MATRIX* src, MATRIX* dest){
  int i,j;
  for(i=0;i<src->h;i++){
   for(j=0;j<src->w;j++){
     dest->data[j][i] = src->data[j][i];
   }
  }
}
